﻿@{
    ViewData["Title"] = "工单管理";
}

<div   class="box2" paneltitle="检索条件">
    <form id="queryForm" method="post">
        <table formmode="transparent">
            <tr>
              
                <td>
                   工单号:
                </td>
                <td>
                    <input type="text" name="OrderNo" id="OrderNo" clearable="true" />
                </td>
                <td>
                   物料编码:
                </td>
                <td>
                    <input type="text" name="partNo" id="partNo" clearable="true" />
                </td>
                <td>
                  顶层订单:
                </td>
                <td>
                    <input type="text" name="refWrokOrderNo" id="refWrokOrderNo" clearable="true" />
                </td>

            </tr>
            <tr>
                 <td class="style2">
                    工作中心:
                </td>
                <td class="style3" >
                    <select prompt="全部" url="/mes/WorkOrder/GetResourceCode" name="resourceCode" id="resourceCode"> </select>
                </td>

                <td class="style2" >
                    工单类型:
                </td>
                <td class="style3" >
                    <select prompt="全部" url="/mes/WorkOrder/GetOrderType" name="orderType" id="orderType"> </select>
                </td>

                 <td class="style2">
                    工单状态:
                </td>
                <td class="style3" >
                    <select prompt="全部" url="/mes/WorkOrder/GetOrderStatus" name="orderStatus" id="orderStatus"> </select>
                </td>
            </tr>
            <tr>
                <td>
                    计划开始时间:
                </td>
                <td>
                    <input class="date" id="txtProduceStartTimeStart" name="txtProduceStartTimeStart" datefmt="yyyy-MM-dd HH:mm:ss"
                           type="text" />
                </td>
              @*  <td style="width:6%"></td>*@
                <td>
                   &nbsp;&nbsp;&nbsp;至:
                </td>
                <td>
                    <input id="txtProduceStartTimeEnd" class="date" name="txtProduceStartTimeEnd" datefmt="yyyy-MM-dd HH:mm:ss"
                           type="text" />
                </td>
                <td>
                    计划结束时间:
                </td>
                <td>
                    <input class="date" id="txtProduceEndTimeStart" name="txtProduceEndTimeStart" datefmt="yyyy-MM-dd HH:mm:ss"
                           type="text"  />
                </td>
              @*  <td style="width:6%"></td>*@
                <td>
                   &nbsp;&nbsp;&nbsp;至:
                </td>
                <td>
                    <input id="txtProduceEndTimeEnd" class="date" name="txtProduceEndTimeEnd" datefmt="yyyy-MM-dd HH:mm:ss"
                           type="text"/>
                </td>

                <td><button type="button" onclick="searchHandler()"><span class="icon_find">查询</span></button></td>
                <td><button type="button" onclick="resetSearch()"><span class="icon_reload">重置</span></button></td>
            </tr>
            @* <%--<tr>
            <td>
            释放时间:
            </td>
            <td>
            <input class="date" id="txtStartPrintTime" name="txtStartPrintTime" datefmt="yyyy-MM-dd HH:mm:ss"
            type="text" width="153"/>
            </td>
            <td style="width:6%"></td>
            <td>
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;至:
            </td>
            <td>
            <input id="txtEndPrintTime" class="date" name="txtEndPrintTime" datefmt="yyyy-MM-dd HH:mm:ss"
            type="text" width="153"/>
            </td>
            <td></td>
            <td>
            <button type="button" onclick="searchHandler()" id="btnSearch">
            <span class="icon_find">查询</span></button>
            </td>
            <td>

            </td>
            </tr>--%>*@
        </table>
    </form>
</div>
<div>
    <div class="padding_right5">
        <div id="maingrid">
        </div>
    </div>
</div>
<script type="text/javascript">
    var grid;

    //加载客户产线
    //cbCustPDLineList();

    function initComplete() {

        grid = $("#maingrid").quiGrid({
            columns: [
                { display: '工单号', name: 'orderNo', align: 'center', width: "10%", isSort: false },
                { display: '零件号', name: 'partNo', align: 'center', width: "10%", isSort: false },
                { display: '工作中心', name: 'pdlineName', align: 'center', width: "5%", isSort: false },
                { display: '顶层订单号', name: 'refWorkOrderNo', align: 'center', width: "10%", isSort: false  },
                { display: '生产订单号', name: 'workCode', align: 'center', width: "10%" , isSort: false },
                { display: '设备', name: 'equipmentCode', align: 'center', width: "10%", isSort: false },
                { display: '数量', name: 'qty', align: 'center', width: "5%", isSort: false },
                { display: '班组', name: 'classesName', align: 'center', width: "10%", isSort: false },
                { display: '工单类型', name: 'orderTypeName', align: 'center', width: "5%", isSort: false },
                { display: '工单状态', name: 'orderStatusName', align: 'center', width: "5%", isSort: false },
                { display: '开始时间', name: 'produceStartTime', align: 'center', width: "10%", isSort: false },
                //{ display: '车间', name: 'dock', align: 'center', width: 120 },

                { display: '结束时间', name: 'produceEndTime', align: 'center', width: "10%",isSort: false },

            ],
            url: '/mes/WorkOrder/getWorkOrderListPage', sortName: 'a.produce_time', rownumbers: true, checkbox: true,
            height: "100%", width: "100%", percentWidthMode: true, pageSize: 100, usePager: true,
            //顶部图标按钮栏rownumbers
            toolbar: {}
        });

        //载入工具栏图标, hide: true
        loadToolbar("", grid);
        addDoubleClick("", grid);

        //刷新数据
        //RefreshData();
        //refresh(true);
    };

    //双击事件
    function onDblClickRow(rowdata) {
        //top.Dialog.confirm("确定要将此序号（包括当前选择行）之后的信息删除？选择的是前排则只删除前排，否则只删除后排！", function() {
        //    $.post("/AdjustOrder/deleteOrderInfo?ruid=" + rowdata.Ruid + "," + rowdata.CustPdlineDesc,
        //        function(result) {
        //            showMsg(result.message);
        //        },
        //        "json");
        //    grid.loadData();
        //});
    }

    //function adjustLine() {

    //    var rows = grid.getSelectedRows();
    //    var rowsLength = rows.length;

    //    if (rowsLength <= 0) {
    //        top.Dialog.alert("请选择行！");
    //        return;
    //    }


    //    //得到查询参数
    //    //            var query = $("#queryForm").formToArray();
    //    //将查询参数传给grid表格
    //    //            grid.setOptions({ params: query });

    //    //            var SequenceNumber = $("#SequenceNumber").val();

    //    top.Dialog.confirm("确定要变更吗？", function() {
    //        $.post("/AdjustOrder/adjustLine",
    //            //获取选中行
    //            getSelectAllId(grid),
    //            function(result) {
    //                handleResult(result.status);
    //            },
    //            "json");

    //    });
    //}

    function upRow() {

        var rows = grid.getSelectedRows();
        var rowsLength = rows.length;

        if (rowsLength != 1) {
            top.Dialog.alert("请选择行，且只能选择一行！");
            return;
        }

        for (var i = 0; i < rowsLength; i++) {

            if (rows[i].a == 1) {
                top.Dialog.alert("无法更改！");
                return;
            }
        }

        top.Dialog.confirm("确定要变更吗？", function() {
            $.post("/AdjustOrder/upRow",
                //获取选中行
                getSelectId(grid),
                function(result) {
                    handleResult(result.status);
                },
                "json");

        });
    }

    function downRow() {
        var rows = grid.getSelectedRows();
        var rowsLength = rows.length;

        if (rowsLength != 1) {
            top.Dialog.alert("请选择行，且只能选择一行！");
            return;
        }

        top.Dialog.confirm("确定要变更吗？", function() {
            $.post("/AdjustOrder/downRow",
                //获取选中行
                getSelectId(grid),
                function(result) {
                    handleResult(result.status);
                },
                "json");

        });
    }

    //获取所有选中行获取选中行的id 格式为 ids=1
    function getSelectId(grid) {

        var selectedRows = grid.getSelectedRows();
        var selectedRowsLength = selectedRows.length;
        var ruid = "";
        var orderNos="";
        //var a = "";

        for (var i = 0; i < selectedRowsLength; i++) {
           // ruid += selectedRows[i].Ruid + ",";
           // a += selectedRows[i].A + ",";
           orderNos+="'"+selectedRows[i].orderNo+"',";
        }
        return { "orderNos": orderNos, };


    }

    //获取所有选中行获取选中行的id 格式为 ids=1
    function getSelectAllId(grid) {

        var selectedRows = grid.getSelectedRows();
        var selectedRowsLength = selectedRows.length;
     
        var orderNos = "";
        var CustPDLine = "";

        for (var i = 0; i < selectedRowsLength; i++) {
         
            orderNos += "'" + selectedRows[i].orderNo + "',";
        }

        CustPDLine = $("#cbCustPDLine").val();

        var SequenceNumber = $("#SequenceNumber").val();

        return { "orderNos": orderNos, "SequenceNumber": SequenceNumber, "CustPDLine": CustPDLine };

    }

    //查询
    function searchHandler() {

        //            var endOrderId = $("#endOrder_id").val();
        //            var cbCustPDLine = $("#cbCustPDLine").val();

        //得到查询参数
        var query = $("#queryForm").formToArray();
        //将查询参数传给grid表格
        grid.setOptions({ params: query });
        //页号重置为1
        grid.setNewPage(1);
        //重新加载数据
        grid.loadData();
    }

    //重置查询
    function resetSearch() {
        $("#queryForm")[0].reset();
        searchHandler();
    }

    //生成配料
    function onGenerateIngredients(rowid, rowidx) {
       
         var rows = grid.getSelectedRows();
        var rowsLength = rows.length;

        if (rowsLength == 0) {
            top.Dialog.alert("请选择生成配料的记录!");
            return;
        }

        var orderNo="";
        for (var i = 0; i < rowsLength; i++) {

            orderNo += "'" + rows[i].orderNo + "',";
        }
        
        //var ordern = JSON.stringify(getSelectId(grid));
        //alert(ordern);
        //debugger
        top.Dialog.open({
          
            URL: "/mes/WorkOrder/GenerateIngredients?orderNos=" + orderNo,
            Title: "备料选择", Width: 600, Height: 350
        });

    }


    //修改
    function onEdit(rowid) {
        var rows = grid.getSelectedRows();
        var rowsLength = rows.length;

        if (rowsLength == 0) {
            top.Dialog.alert("请选中要编辑的记录!");
            return;
        } else if (rowsLength > 1) {
            top.Dialog.alert("只能选择一行编辑");
            return;
        }
        rowid = rows[0].menuId;
        top.Dialog.open({
            URL: "/wms/Menu/editMenu?menuId=" + rowid,
            Title: "修改", Width: 600, Height: 500
        });
    }



    

      //启动结果
    function FiringhandleResult(result) {

        if (result != -1 && result != 0) {
            top.Dialog.alert("启动成功！" + result + "条数据", null, null, null, 1);
            grid.loadData();
        } else {
            top.Dialog.alert("启动失败！");
        }
    }
    //启动
    function onFiring(rowid, rowidx) {

        var rows = grid.getSelectedRows();
        var rowsLength = rows.length;

        if (rowsLength == 0) {
            top.Dialog.alert("请选择启动的记录!");
            return;
        }
        top.Dialog.confirm("确定要启动吗？", function () {
            $.post("/mes/WorkOrder/onFiring",
                //获取所有选中行
                getSelectId(grid),
                function(result) {
                    FiringhandleResult(result.status);
                },
                "json");
        });

    }

       //暂停结果
    function SuspendhandleResult(result) {
        if (result != -1 && result != 0) {
            top.Dialog.alert("暂停成功！" + result + "条数据", null, null, null, 1);
            grid.loadData();
        } else {
            top.Dialog.alert("暂停失败！");
        }
    }


    //暂停
    function onSuspend(result) {
       
         var rows = grid.getSelectedRows();
        var rowsLength = rows.length;

        if (rowsLength == 0) {
            top.Dialog.alert("请选择暂停的记录!");
            return;
        }
        top.Dialog.confirm("确定要暂停吗？", function () {
            $.post("/mes/WorkOrder/onSuspend",
                //获取所有选中行
                getSelectId(grid),
                function(result) {
                    SuspendhandleResult(result.status);
                },
                "json");
        });


    }

   //取消结果
    function CancelhandleResult(result) {
        if(result != -1 && result != 0) {
            top.Dialog.alert("取消成功！" + result + "条数据", null, null, null, 1);
            grid.loadData();
        } else {
            top.Dialog.alert("取消失败！");
        }
    }

    //取消
    function onCancel(result) {
       
        var rows = grid.getSelectedRows();
        var rowsLength = rows.length;

        if (rowsLength == 0) {
            top.Dialog.alert("请选择取消的记录!");
            return;
        }
        top.Dialog.confirm("确定要取消吗？", function () {
            $.post("/mes/WorkOrder/onCancel",
                //获取所有选中行
                getSelectId(grid),
                function(result) {
                    CancelhandleResult(result.status);
                },
                "json");
        });

    }

     //关闭结果
    function ClosehandleResult(result) {
        if (result != -1 && result != 0) {
            top.Dialog.alert("关闭成功！" + result + "条数据", null, null, null, 1);
            grid.loadData();
        } else {
            top.Dialog.alert("关闭失败！");
        }
    }

    //关闭
    function onClose(result) {
       
        var rows = grid.getSelectedRows();
        var rowsLength = rows.length;

        if (rowsLength == 0) {
            top.Dialog.alert("请选择关闭的记录!");
            return;
        }
        top.Dialog.confirm("确定要关闭吗？", function () {
            $.post("/mes/WorkOrder/onClose",
                //获取所有选中行
                getSelectId(grid),
                function(result) {
                    ClosehandleResult(result.status);
                },
                "json");
        });


    }

     //完成结果
    function AccomplishhandleResult(result) {
       if(result != -1 && result != 0) {
            top.Dialog.alert("完成成功！" + result + "条数据", null, null, null, 1);
            grid.loadData();
        } else {
            top.Dialog.alert("完成失败！");
        }
    }

    //完成
    function onAccomplish(result) {
       
        var rows = grid.getSelectedRows();
        var rowsLength = rows.length;

        if (rowsLength == 0) {
            top.Dialog.alert("请选择完成的记录!");
            return;
        }
        top.Dialog.confirm("确定要完成吗？", function () {
            $.post("/mes/WorkOrder/onAccomplish",
                //获取所有选中行
                getSelectId(grid),
                function(result) {
                   AccomplishhandleResult(result.status);
                },
                "json");
        });


    }













    //添加
    function addCustomerOrder() {
        top.Dialog.open({ URL: "/Views/AdjustOrder/editCustomerOrder.aspx", Title: "新增", Width: 500, Height: 400 });
    }

    //批量删除
    function deleteOrder() {
        var rows = grid.getSelectedRows();
        var rowsLength = rows.length;

        if (rowsLength == 0) {
            top.Dialog.alert("请选中要删除的记录!");
            return;
        }
        top.Dialog.confirm("确定要删除吗？", function() {
            $.post("/AdjustOrder/deleteOrder",
                //获取所有选中行
                getSelectId(grid),
                function(result) {
                    showMsg(result.message);
                },
                "json");
            grid.loadData();
        });
    }

    //删除后的提示
    function handleResult(result) {
        if (result == false) {
            showMsg("更改失败！");
        } else if (result == true) {
            showMsg("更改成功！");
            grid.loadData();
        }
    }

        //获取所有选中行获取选中行的id 格式为 ids=1&ids=2
    function getSelectById(grid) {
        var selectedRows = grid.getSelectedRows();
        var selectedRowsLength = selectedRows.length;
        var ids = "";

        for (var i = 0; i < selectedRowsLength; i++) {
            ids += selectedRows[i].ruid + ",";
        }
        return { "ids": ids };
    }

    //启用
    function EnablehandleResult(result) {
        if (result != -1) {
            top.Dialog.alert("启用成功！启用" + result + "条数据", null, null, null, 1);
            grid.loadData();
        } else {
            top.Dialog.alert("启用失败！");
        }
    }

   
    //禁用
    function DisablehandleResult(result) {
        if (result != -1) {
            top.Dialog.alert("禁用成功！禁用" + result + "条数据", null, null, null, 1);
            grid.loadData();
        } else {
            top.Dialog.alert("禁用失败！");
        }
    }
    //启用
    function onEnable(rowid, rowidx) {
        var rows = grid.getSelectedRows();
        var rowsLength = rows.length;

        if (rowsLength == 0) {
            top.Dialog.alert("请选中要启用的记录!");
            return;
        }
        $.post("/AdjustOrder/onEnable",
            //获取所有选中行
            getSelectById(grid),
            function(result) {
                EnablehandleResult(result.status);
            },
            "json");
    }
    //禁用
    function onDisable(rowid, rowidx) {
        var rows = grid.getSelectedRows();
        var rowsLength = rows.length;

        if (rowsLength == 0) {
            top.Dialog.alert("请选中要禁用的记录!");
            return;
        }
        top.Dialog.confirm("确定要禁用吗？", function() {
            $.post("/AdjustOrder/onDisable",
                //获取所有选中行
                getSelectById(grid),
                function(result) {
                    DisablehandleResult(result.status);
                },
                "json");
        });
    }


    //右下角提示信息
    function showMsg(message) {
        $.messager.show(0, message, 5000);
    }

    //刷新表格数据并重置排序和页数
    function refresh(isUpdate) {
        if (!isUpdate) {
            //重置排序
            grid.options.sortName = 'a.partId';
            //页号重置为1
            grid.setNewPage(1);
        }
        //            grid.loadData();
        RefreshData();
    }

    //        function cbCustPDLineList() {
    //            $.post("/ProductionOrderRelease/GetCustPDLine", {}, function (result) {
    //                $("#cbCustPDLine").data("data", result);
    //                $("#cbCustPDLine").render();
    //                //                RefreshData();
    //            }, "json");

    //        }

    //刷新数据
    function RefreshData() {
        //            var query = { "cbCustPDLine": $("#cbCustPDLine").val() };
        //            //将查询参数传给grid表格
        //            grid.setOptions({ params: query });
        //重新加载数据
        grid.loadData();
    }
    //导出
    function exportModelType() {
        exportData(true);
    }

    //导出所有
    function exportModelType2() {
        exportData(false);
    }

    //导出处理
    function exportData(isPage) {

        var pageNo = grid.options.page;
        var pagerSize = grid.options.pageSize;;
        var sort = grid.options.sortName;
        var direction = grid.options.sortOrder;
        var cbCustPDLine = $("#cbCustPDLine").val();
        var SN = $("#SN").val();
        var partNo = $("#partNo").val();

        var url = "/AdjustOrder/exportData";
        if (isPage) {

            url += "?pager.pageSize=" + pagerSize;
            url += "&pager.pageNo=" + pageNo;
            url += "&sort=" + sort;
            url += "&direction=" + direction;
            url += "&isPage=1";


        } else {
            var rowsCount = grid.options.total;

            url += "?isPage=0";
            url += "&pager.pageSize=" + rowsCount;
            url += "&pager.pageNo=" + pageNo;
            url += "&sort=" + sort;
            url += "&direction=" + direction;

        }
        url += " &cbCustPDLine=" + cbCustPDLine, " &SN=" + SN, " &partNo=" + partNo;

        window.location = url;
    }
</script>
